*For coholders
eststo a: quietly estpost summarize ca_count_mean sa_count_mean ccard_count_mean if coholder==0 & edate_id==1335, detail
eststo b: quietly estpost summarize ca_count_mean sa_count_mean ccard_count_mean if coholder==1 & edate_id==1335, detail
eststo diff: estpost ttest ca_count_mean sa_count_mean ccard_count_mean if coholder==1 & edate_id==1335 | coholder==0 & edate_id==1335, by(coholder)

esttab a b diff using "Tables/NrCACountCoholderNon.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.1fc)) p25(label(P25) pattern(1 1 0) fmt(%15.1fc)) p50(label(P50) pattern(1 1 0) fmt(%15.1fc)) p75(label(P75) pattern(1 1 0) fmt(%15.1fc)) p90(label(P90) pattern(1 1 0) fmt(%15.1fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.1fc)) se(label(SE) par pattern(0 0 1) fmt(%15.1fc))") ///
mtitle("No coholder" "Coholder" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
ca_count_mean "Number of current accounts" ///
sa_count_mean "Number of savings accounts" ///
ccard_count_mean "Number of credit cards") ///
substitute(? \) 

*For frequent coholders
eststo a: quietly estpost summarize ca_count_mean sa_count_mean ccard_count_mean if frequent_coholder==0 & edate_id==1335, detail
eststo b: quietly estpost summarize ca_count_mean sa_count_mean ccard_count_mean if frequent_coholder==1 & edate_id==1335, detail
eststo diff: estpost ttest ca_count_mean sa_count_mean ccard_count_mean if frequent_coholder==1 & edate_id==1335 | frequent_coholder==0 & edate_id==1335, by(frequent_coholder)

esttab a b diff using "Tables/NrCACountCoholderNon_frequent.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.1fc)) p25(label(P25) pattern(1 1 0) fmt(%15.1fc)) p50(label(P50) pattern(1 1 0) fmt(%15.1fc)) p75(label(P75) pattern(1 1 0) fmt(%15.1fc)) p90(label(P90) pattern(1 1 0) fmt(%15.1fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.1fc)) se(label(SE) par pattern(0 0 1) fmt(%15.1fc))") ///
mtitle("No frequent coholder" "Frequent coholder" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
ca_count_mean "Number of current accounts" ///
sa_count_mean "Number of savings accounts" ///
ccard_count_mean "Number of credit cards") ///
substitute(? \) 

*For intense coholders
eststo a: quietly estpost summarize ca_count_mean sa_count_mean ccard_count_mean if intense_coholder==0 & edate_id==1335, detail
eststo b: quietly estpost summarize ca_count_mean sa_count_mean ccard_count_mean if intense_coholder==1 & edate_id==1335, detail
eststo diff: estpost ttest ca_count_mean sa_count_mean ccard_count_mean if intense_coholder==1  & edate_id==1335 | intense_coholder==0 & edate_id==1335, by(intense_coholder)

esttab a b diff using "Tables/NrCACountCoholderNon_intense.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.1fc)) p25(label(P25) pattern(1 1 0) fmt(%15.1fc)) p50(label(P50) pattern(1 1 0) fmt(%15.1fc)) p75(label(P75) pattern(1 1 0) fmt(%15.1fc)) p90(label(P90) pattern(1 1 0) fmt(%15.1fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.1fc)) se(label(SE) par pattern(0 0 1) fmt(%15.1fc))") ///
mtitle("No intense coholder" "Intense coholder" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
ca_count_mean "Number of current accounts" ///
sa_count_mean "Number of savings accounts" ///
ccard_count_mean "Number of credit cards") ///
substitute(? \) 

*For costly coholders
eststo a: quietly estpost summarize ca_count_mean sa_count_mean ccard_count_mean if costly_coholder==0 & edate_id==1335, detail
eststo b: quietly estpost summarize ca_count_mean sa_count_mean ccard_count_mean if costly_coholder==1 & edate_id==1335, detail
eststo diff: estpost ttest ca_count_mean sa_count_mean ccard_count_mean if ///
costly_coholder==1  & edate_id==1335 | costly_coholder==0 & edate_id==1335, ///
by(costly_coholder)

esttab a b diff using "Tables/NrCACountCoholderNon_costly.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.1fc)) p25(label(P25) pattern(1 1 0) fmt(%15.1fc)) p50(label(P50) pattern(1 1 0) fmt(%15.1fc)) p75(label(P75) pattern(1 1 0) fmt(%15.1fc)) p90(label(P90) pattern(1 1 0) fmt(%15.1fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.1fc)) se(label(SE) par pattern(0 0 1) fmt(%15.1fc))") ///
mtitle("No costly coholder" "Costly coholder" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
ca_count_mean "Number of current accounts" ///
sa_count_mean "Number of savings accounts" ///
ccard_count_mean "Number of credit cards") ///
substitute(? \) 

*For cohold day given being coholder
eststo a: quietly estpost summarize ca_count sa_count ccard_count if cohold_day==0 & coholder==1, detail
eststo b: quietly estpost summarize ca_count sa_count ccard_count if cohold_day==1 & coholder==1, detail
eststo diff: quietly estpost ttest ca_count sa_count ccard_count if cohold_day==1 & coholder==1 | cohold_day==0 & coholder==1, by(cohold_day)

esttab a b diff using "Tables/NrCACountCoholdDays.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.1fc)) p25(label(P25) pattern(1 1 0) fmt(%15.1fc)) p50(label(P50) pattern(1 1 0) fmt(%15.1fc)) p75(label(P75) pattern(1 1 0) fmt(%15.1fc)) p90(label(P90) pattern(1 1 0) fmt(%15.1fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.1fc)) se(label(SE) par pattern(0 0 1) fmt(%15.1fc))") ///
mtitle("\shortstack{Coholder: \\ No cohold day}" "\shortstack{Coholder:\\ Cohold day}" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
ca_count "Number of current accounts" ///
sa_count "Number of savings accounts" ///
ccard_count "Number of credit cards") ///
substitute(? \) 

*For cohold day given being frequent coholder
eststo a: quietly estpost summarize ca_count sa_count ccard_count if cohold_day==0 & frequent_coholder==1, detail
eststo b: quietly estpost summarize ca_count sa_count ccard_count if cohold_day==1 & frequent_coholder==1, detail
eststo diff: quietly estpost ttest ca_count sa_count ccard_count if cohold_day==1 & frequent_coholder==1 | cohold_day==0 & frequent_coholder==1, by(cohold_day)

esttab a b diff using "Tables/NrCACountCoholdDays_frequent.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.1fc)) p25(label(P25) pattern(1 1 0) fmt(%15.1fc)) p50(label(P50) pattern(1 1 0) fmt(%15.1fc)) p75(label(P75) pattern(1 1 0) fmt(%15.1fc)) p90(label(P90) pattern(1 1 0) fmt(%15.1fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.1fc)) se(label(SE) par pattern(0 0 1) fmt(%15.1fc))") ///
mtitle("\shortstack{Frequent coholder: \\ No cohold day}" "\shortstack{Frequent coholder:\\ Cohold day}" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
ca_count "Number of current accounts" ///
sa_count "Number of savings accounts" ///
ccard_count "Number of credit cards") ///
substitute(? \) 

*For cohold day given being intense coholder
eststo a: quietly estpost summarize ca_count sa_count ccard_count if cohold_day==0 & intense_coholder==1, detail
eststo b: quietly estpost summarize ca_count sa_count ccard_count if cohold_day==1 & intense_coholder==1, detail
eststo diff: quietly estpost ttest ca_count sa_count ccard_count if cohold_day==1 & intense_coholder==1 | cohold_day==0 & intense_coholder==1, by(cohold_day)

esttab a b diff using "Tables/NrCACountCoholdDays_intense.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.1fc)) p25(label(P25) pattern(1 1 0) fmt(%15.1fc)) p50(label(P50) pattern(1 1 0) fmt(%15.1fc)) p75(label(P75) pattern(1 1 0) fmt(%15.1fc)) p90(label(P90) pattern(1 1 0) fmt(%15.1fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.1fc)) se(label(SE) par pattern(0 0 1) fmt(%15.1fc))") ///
mtitle("\shortstack{Intense coholder: \\ No cohold day}" "\shortstack{Intense coholder: \\ Cohold day}" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
ca_count "Number of current accounts" ///
sa_count "Number of savings accounts" ///
ccard_count "Number of credit cards") ///
substitute(? \) 

*For cohold day given being costly coholder
eststo a: quietly estpost summarize ca_count sa_count ccard_count if cohold_day==0 & costly_coholder==1, detail
eststo b: quietly estpost summarize ca_count sa_count ccard_count if cohold_day==1 & costly_coholder==1, detail
eststo diff: quietly estpost ttest ca_count sa_count ccard_count if cohold_day==1 & costly_coholder==1 | cohold_day==0 & intense_coholder==1, by(cohold_day)

esttab a b diff using "Tables/NrCACountCoholdDays_costly.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.1fc)) p25(label(P25) pattern(1 1 0) fmt(%15.1fc)) p50(label(P50) pattern(1 1 0) fmt(%15.1fc)) p75(label(P75) pattern(1 1 0) fmt(%15.1fc)) p90(label(P90) pattern(1 1 0) fmt(%15.1fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.1fc)) se(label(SE) par pattern(0 0 1) fmt(%15.1fc))") ///
mtitle("\shortstack{Costly coholder: \\ No cohold day}" "\shortstack{Costly coholder: \\ Cohold day}" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
ca_count "Number of current accounts" ///
sa_count "Number of savings accounts" ///
ccard_count "Number of credit cards") ///
substitute(? \)

*For coholding cycle
eststo a: quietly estpost summarize ca_count sa_count ccard_count if ///
	cohold_spell_cat==0 & coholder==1 & cohold_spell_cat_uoa==1
eststo b: quietly estpost summarize ca_count sa_count ccard_count if ///
	cohold_spell_cat==1 & coholder==1 & cohold_spell_cat_uoa==1
eststo c: quietly estpost summarize ca_count sa_count ccard_count if ///
	cohold_spell_cat==2 & coholder==1 & cohold_spell_cat_uoa==1
eststo d: quietly estpost summarize ca_count sa_count ccard_count if ///
	cohold_spell_cat==3 & coholder==1 & cohold_spell_cat_uoa==1
eststo diff: quietly estpost ttest ca_count sa_count ccard_count if ///
cohold_spell_cat==0 & coholder==1 & cohold_spell_cat_uoa==1 | ///
cohold_spell_cat==3 & coholder==1 & cohold_spell_cat_uoa==1, by(cohold_day)

esttab a b c d diff using "Tables/NrCACountCoholdDays_cycle.tex", replace ///
cells("mean(label(Mean) pattern(1 1 1 1 0) fmt(%15.1fc)) b(label(Diff.) star pattern(0 0 0 0 1) fmt(%15.1fc)) se(label(SE) par pattern(0 0 0 0 1) fmt(%15.1fc))") ///
mtitle("None" "Start" "Intermediate" "End" "Diff None-Intermediate") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
ca_count "Number of current accounts" ///
sa_count "Number of savings accounts" ///
ccard_count "Number of credit cards") ///
substitute(? \) 

********************************************************************************


foreach y in ca_count_rel_spell sa_count_rel_spell ccard_count_rel_spell {

	if "`y'" == "ca_count_rel_spell" {
	local title = "Nr. current accounts"
	}
	if "`y'" == "sa_count_rel_spell" {
	local title = "Nr. savings accounts"
	}
	if "`y'" == "ccard_count_rel_spell" {
	local title = "Nr. credit cards"
	}

	local category "cohold_spell_cat cohold_spell_cat_frequent cohold_spell_cat_intense cohold_spell_cat_costly"
	gen subcategory = ""
	gen mean = .
	gen lqt = .
	gen uqt = .
	gen n = _n
	local i = 1

		foreach var in `category' {
		if "`var'" == "cohold_spell_cat" | "`var'" == "cohold_spell_cat_frequent" | "`var'" == "cohold_spell_cat_intense" | "`var'" == "cohold_spell_cat_costly" {
			local min 0
			local max 5
		} 
		
		 forvalues j = `min'/`max' { 
			replace subcategory = "`var'`j'" if n == `i'    
			ci means `y' if `var' == `j' & coholder==1 & cohold_spell_cat_uoa==1, level(90)
			gen m = `r(mean)'
			gen l = `r(lb)'
			gen u = `r(ub)'
			replace mean = m if subcategory == "`var'`j'"
			replace lqt  = l if subcategory == "`var'`j'"
			replace uqt  = u if subcategory == "`var'`j'"
			local i = `i' + 1
			drop m l u
		 }
		local i = `i' + 1	 
	}

	sort n
	egen min = min(lqt*10)
	egen max = max(uqt*10)

	twoway ///
	rcap uqt lqt n if subcategory != "", lstyle(ci) ||  ///
	 scatter mean n if subcategory != "", ///
	 mcolor(blue) mstyle(p1) ///
	 title("`title' relative to individual mean", place(left) size(large)) ///
	 ytitle("") ///
	 xtitle("")  ///
	 xline(7 14 21, lpattern(dot)) ///
	 ylabel(, labsize(medium) angle(0) format(%9.2fc)) /// 
	 xlabel(0.7 `""Coholder:" "No cohold day""' 	    2 "Start"  3 "First 1/3"  4 "Intermediate 1/3"  5 "Last 1/3"  6 "End" ///
		7.7 `""Frequent coholder:" "No cohold day""'  9 "Start" 10 "First 1/3" 11 "Intermediate 1/3" 12 "Last 1/3" 13 "End" ///
		14.7 `""Intense coholder:" "No cohold day""' 16 "Start" 17 "First 1/3" 18 "Intermediate 1/3" 19 "Last 1/3" 20 "End" ///
		21.7 `""Costly coholder:" "No cohold day""' 23 "Start" 24 "First 1/3" 25 "Intermediate 1/3" 26 "Last 1/3" 27 "End", ///
	 labsize(medsmall) angle(90))  ///
	 xscale(range(0.25 27.75)) ///
	 legend(off)
	 
drop subcategory mean lqt uqt n 
drop min max  	 
graph export "Figures/CohExpl_Mental_`y'.pdf", replace
graph export "Figures/CohExpl_Mental_`y'.png", replace
}


*Scatterplot nr of accounts rel to individual mean vs unnecessary overdraft rel to individual mean
binscatter ca_count_rel unnec_od_rel if coholder==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Number of current accounts relative to mean", place(left) size(large)) ///
ytitle("") ///
xtitle("Unnecessary overdraft relative to mean", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) format(%9.2fc) angle(0)) ///
xlabel(, labsize(medium)) 
graph export "Figures/CohExp_Mental_AccountsUnnecODScatter.pdf", replace
graph export "Figures/CohExp_Mental_AccountsUnnecODScatter.png", width(1800) height(1300) replace




********************************************************************************
*Expenditure supercategories, expenditure categories, financial aggregates
*by coholding cycle


*Create figure for each variable in loop	
foreach var in ///
	cr_utiliz ///
	dur_ex_exp nondur_ex_exp ///
	ca_count_rel sa_count_rel ccard_count_rel ///
	dur_unnec_od dur_ex_unnec_od nondur_unnec_od nondur_ex_unnec_od ///
	tempt_unnec_od ///
	dur_exp nondur_exp tempt_exp ///
	cash_exp od_amount_exp unnec_od_exp ///
	total_exp_exp nec_exp unnec_exp ///
	groc_exp fuel_exp alc_exp rmf_exp hi_exp hs_exp transp_exp ca_exp pharm_exp ///
	recr_exp lottery_exp_exp gambling_exp_exp gaming_exp charities_exp ///
	special_exp sa_exp ts_exp holiday_exp ///
	total_income_rel reg_income_rel salary_rel irr_income_rel ///
	sa_bal_rel ca_bal_rel ccard_bal_rel ///
	sa_lim_rel ca_lim_rel ccard_lim_rel ///
	drattarv_rel fit_rel utvextir_rel total_fin_cost_rel ///
	od_amount_rel cash_rel ///
	liquidity_rel unnec_od_rel ///
	pay_day income_day windfall_dum new_car {
bysort cohold_spell_cat: egen mean = mean(`var'_spell) if ///
				coholder==1 & cohold_spell_cat_uoa==1
bysort cohold_spell_cat: egen std = sd(`var'_spell) if ///
				coholder==1 & cohold_spell_cat_uoa==1
bysort cohold_spell_cat: egen obs = count(dum) if ///
				coholder==1 & cohold_spell_cat_uoa==1
gen se = std/sqrt(obs) if 	coholder==1 & cohold_spell_cat_uoa==1
bysort cohold_spell_cat: egen n = seq() if ///
				coholder==1 & cohold_spell_cat_uoa==1

if "`var'" == "cr_utiliz" {
local title "Credit card balance relative to limit"
local format "%9.2fc"
}
else if "`var'" == "ca_count_rel" {
local title "Number of current accounts"
local format "%9.2fc"
}
else if "`var'" == "sa_count_rel" {
local title "Number of savings accounts"
local format "%9.2fc"
}
else if "`var'" == "ccard_count_rel" {
local title "Number of credit cards"
local format "%9.2fc"
}
else if "`var'" == "dur_unnec_od" {
local title "Durable goods relative to unnecessary overdraft"
local format "%9.2fc"
}
else if "`var'" == "dur_ex_unnec_od" {
local title "Durable goods (excl.) relative to unnecessary overdraft"
local format "%9.2fc"
}
else if "`var'" == "nondur_unnec_od" {
local title "Non-durable goods relative to unnecessary overdraft"
local format "%9.2fc"
}
else if "`var'" == "nondur_ex_unnec_od" {
local title "Non-durable goods (excl.) relative to unnecessary overdraft"
local format "%9.2fc"
}
else if "`var'" == "tempt_unnec_od" {
local title "Temptation goods relative to unnecessary overdraft"
local format "%9.2fc"
}
else if "`var'" == "dur_exp" {
local title "Durable goods"
local format "%9.2fc"
}
else if "`var'" == "dur_ex_exp" {
local title "Durable goods (exclusively)"
local format "%9.2fc"
}
else if "`var'" == "nondur_exp" {
local title "Nondurable goods"
local format "%9.2fc"
}
else if "`var'" == "nondur_ex_exp" {
local title "Nondurable goods (exclusively)"
local format "%9.2fc"
}
else if "`var'" == "tempt_exp" {
local title "Temptation goods"
local format "%9.2fc"
}
else if "`var'" == "cash_exp" {
local title "Cash"
local format "%9.0fc"
}
else if "`var'" == "od_amount_exp" {
local title "Overdraft"
local format "%9.0fc"
}
else if "`var'" == "unnec_od_exp" {
local title "Unnecessary overdraft"
local format "%9.0fc"
}
else if "`var'" == "total_exp_exp" {
local title "Total expenditures"
local format "%9.1fc"
}
else if "`var'" == "nec_exp" {
local title "Necessities"
local format "%9.2fc"
}
else if "`var'" == "unnec_exp" {
local title "Non-necessities"
local format "%9.2fc"
}
else if "`var'" == "groc_exp" {
local title "Groceries"
local format "%9.2fc"
}
else if "`var'" == "fuel_exp" {
local title "Fuel"
local format "%9.2fc"
}
else if "`var'" == "alc_exp" {
local title "Alcohol"
local format "%9.3fc"
}
else if "`var'" == "rmf_exp" {
local title "Ready made food"
local format "%9.2fc"
}
else if "`var'" == "hi_exp" {
local title "Home improvements"
local format "%9.2fc"
}
else if "`var'" == "hs_exp" {
local title "Home security"
local format "%9.3fc"
}
else if "`var'" == "transp_exp" {
local title "Transportation"
local format "%9.2fc"
}
else if "`var'" == "ca_exp" {
local title "Clothes and accessories"
local format "%9.2fc"
}
else if "`var'" == "pharm_exp" {
local title "Pharmaceuticals"
local format "%9.3fc"
}
else if "`var'" == "recr_exp" {
local title "Recreation"
local format "%9.2fc"
}
else if "`var'" == "lottery_exp_exp" {
local title "Lottery expenditures"
local format "%9.3fc"
}
else if "`var'" == "gambling_exp_exp" {
local title "Gambling expenditures"
local format "%9.3fc"
}
else if "`var'" == "gaming_exp" {
local title "Gaming expenditures"
local format "%9.3fc"
}
else if "`var'" == "charities_exp" {
local title "Charities"
local format "%9.3fc"
}
else if "`var'" == "special_exp" {
local title "Special expenditures"
local format "%9.3fc"
}
else if "`var'" == "sa_exp" {
local title "Sports and activities"
local format "%9.3fc"
}
else if "`var'" == "ts_exp" {
local title "TS"
local format "%9.3fc"
}
else if "`var'" == "holiday_exp" {
local title "Holiday"
local format "%9.4fc"
}
else if "`var'" == "new_car" {
local title "Probability of getting new car"
local format "%9.4fc"
}

else if "`var'" == "total_income_rel" {
local title = "Total income"
local format "%9.2fc"
}
else if "`var'" == "reg_income_rel" {
local title = "Regular income"
local format "%9.2fc"
}
else if "`var'" == "salary_rel" {
local title = "Salary"
local format "%9.2fc"
}
else if "`var'" == "irr_income_rel" {
local title = "Irregular income"
local format "%9.2fc"
}
else if "`var'" == "sa_bal_rel" {
local title = "Savings account balance"
local format "%9.2fc"
}
else if "`var'" == "ca_bal_rel" {
local title = "Current account balance"
local format "%9.2fc"
}
else if "`var'" == "ccard_bal_rel" {
local title = "Credit card balance"
local format "%9.2fc"
}
else if "`var'" == "sa_lim_rel" {
local title = "Savings account limit"
local format "%9.2fc"
}
else if "`var'" == "ca_lim_rel" {
local title = "Current account limit"
local format "%9.2fc"
}
else if "`var'" == "ccard_lim_rel" {
local title = "Credit card limit"
local format "%9.2fc"
}
else if "`var'" == "drattarv_rel" {
local title = "Late-payment interests"
local format "%9.2fc"
}
else if "`var'" == "fit_rel" {
local title = "Non-sufficient funds fees"
local format "%9.2fc"
}
else if "`var'" == "utvextir_rel" {
local title = "Overdraft interest"
local format "%9.2fc"
}
else if "`var'" == "total_fin_cost_rel" {
local title = "Total financial costs"
local format "%9.2fc"
}
else if "`var'" == "od_amount_rel" {
local title = "Overdraft"
local format "%9.2fc"
}
else if "`var'" == "cash_rel" {
local title = "Cash"
local format "%9.2fc"
}
else if "`var'" == "liquidity_rel" {
local title = "Liquidity"
local format "%9.2fc"
}
else if "`var'" == "unnec_od_rel" {
local title = "Unnecessary overdraft"
local format "%9.2fc"
}
else if "`var'" == "pay_day" {
local title = "Probability of receiving regular income"
local format "%9.2fc"
}
else if "`var'" == "income_day" {
local title = "Probability of receiving any income"
local format "%9.2fc"
}
else if "`var'" == "windfall_dum" {
local title = "Probability of receiving a windfall"
local format "%9.4fc"
}

serrbar mean se cohold_spell_cat if n ==1, /// 
mvopts(mcolor(blue)) ///
scale(1.64) ///
title("`title'", place(left) size(large)) ///
ytitle("") ///
xtitle("Point in coholding cycle", size(large)) ///
ylabel(, labsize(large) angle(0) format("`format'")) ///
xlabel(0 "No cohold day" 1 "Start" 2 "First 1/3" 3 "Intermediate 1/3" 4 "Last 1/3" 5 "End", ///
	labsize(medium)) ///
xscale(range(-0.25 5.25)) 
graph export "Figures/CohExpl_Mental_`var'.pdf", replace
graph export "Figures/CohExpl_Mental_`var'.png", replace
drop mean std obs se n
}

***Probability of coholding starting depending on the type of expenditures
*group 1: only durable expenditures are positive
*group 2: only nondurabe expendiures are positive
gen pr_dur_rel_non = .
replace pr_dur_rel_non = 1 if dur_ex > 0 & dur_ex !=.
replace pr_dur_rel_non = 0 if nondur_ex > 0 & nondur_ex !=. 

eststo a: estpost summarize ///
cohold_day pr_cohold_start pr_cohold_end ///
if pr_dur_rel_non == 0 & coholder==1, detail

eststo b: estpost summarize ///
cohold_day pr_cohold_start pr_cohold_end ///
if pr_dur_rel_non == 1 & coholder==1, detail

eststo diff: quietly estpost ttest ///
cohold_day pr_cohold_start pr_cohold_end ///
if pr_dur_rel_non==1 & coholder==1 | pr_dur_rel_non==0 & coholder==1, ///
by(pr_dur_rel_non)

esttab a b diff using "Tables/Coh_Expl_MentalProbCoholdingDurNonDur.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.3fc)) p25(label(P25) pattern(1 1 0) fmt(%15.3fc)) p50(label(P50) pattern(1 1 0) fmt(%15.3fc)) p75(label(P75) pattern(1 1 0) fmt(%15.3fc)) p90(label(P90) pattern(1 1 0) fmt(%15.3fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.3fc)) se(label(SE) par pattern(0 0 1) fmt(%15.3fc))") ///
mtitle("Non-durable expenditures (excl.)" "Durable expenditures (excl.)" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
cohold_day "Cohold day" pr_cohold_start "Coholding starts" pr_cohold_end "Coholding ends" ///
) ///
substitute(? \)

	
***************************************************************************
*Table 5: Excess Transactions on Overdraft Accounts
***************************************************************************	
egen month_total_income_mean_median = median(month_total_income_mean)
egen age_median = median(age)
generate income_dum = 0
replace income_dum = 1 if month_total_income_mean >= month_total_income_mean_median
generate age_dum = 0
replace age_dum = 1 if age >= age_median
gen gambling_total = lottery_exp + gambling_exp
gen od_dum = 0
replace od_dum = 1 if od_exp != 0

// Whole sample 
foreach var in total_exp nec unnec groc fuel pharm ca alc rmf group recr gambling_total gaming cinema books craft fish recr_area special sa ts swim toys hi hs transp media charities new_car holiday tempt{
	gen `var'_dum1 = 0
	gen `var'_dum2 = 0
	replace `var'_dum1 = 1 if `var'!=0
	replace `var'_dum2 = 1 if `var'!=0 & od_exp !=0
	egen `var'_dum1_num = total(`var'_dum1 == 1)
	egen `var'_dum2_num = total(`var'_dum2 == 1)
	gen separate_`var' = `var'_dum2_num*100/`var'_dum1_num
}
keep if _n == 1
outreg2 using summary1.doc, replace sum(detail) keep(separate_*) eqkeep(N mean)

// Subsample by age
forvalues i =0/1{
preserve
keep if age_dum ==`i'
foreach var in groc fuel pharm ca alc rmf group recr gambling_total gaming cinema books craft special sa swim toys hi hs transp media charities holiday tempt{
	gen `var'_dum1 = 0
	gen `var'_dum2 = 0
	replace `var'_dum1 = 1 if `var'!=0
	replace `var'_dum2 = 1 if `var'!=0 & od_exp !=0
	egen `var'_dum1_num = total(`var'_dum1 == 1)
	egen `var'_dum2_num = total(`var'_dum2 == 1)
	gen separate_`var' = `var'_dum2_num*100/`var'_dum1_num
}
keep if _n == 1
outreg2 using summary3`i'.doc, replace sum(detail) keep(separate_*) eqkeep(N mean)
restore
}

// Subsample by income
forvalues i =0/1{
preserve
keep if income_dum ==`i'
foreach var in groc fuel pharm ca alc rmf group recr gambling_total gaming cinema books craft special sa swim toys hi hs transp media charities holiday tempt{
	gen `var'_dum1 = 0
	gen `var'_dum2 = 0
	replace `var'_dum1 = 1 if `var'!=0
	replace `var'_dum2 = 1 if `var'!=0 & od_exp !=0
	egen `var'_dum1_num = total(`var'_dum1 == 1)
	egen `var'_dum2_num = total(`var'_dum2 == 1)
	gen separate_`var' = `var'_dum2_num*100/`var'_dum1_num
}
keep if _n == 1
outreg2 using summary3`i'.doc, replace sum(detail) keep(separate_*) eqkeep(N mean)
restore
}

***************************************************************************
*Table A18: Excess Transactions on Overdraft Accounts by Expenditure Value
***************************************************************************
// Whole sample 
foreach var in total_exp nec unnec groc fuel pharm ca alc rmf group recr gambling_total gaming cinema books craft fish recr_area special sa ts swim toys hi hs transp media charities new_car holiday tempt{
	egen `var'_sum = total(`var')
	egen `var'_sum_od = total(`var') if od_dum == 1 
	gen pct_`var' = `var'_sum_od*100/`var'_sum
}
keep if _n == 1
outreg2 using summary4.doc, replace sum(detail) keep(pct_*) eqkeep(N mean)


// Subsample by age
forvalues i =0/1{
preserve
keep if age_dum ==`i'
foreach var in groc fuel pharm ca alc rmf group recr gambling_total gaming cinema books craft fish recr_area special sa ts swim toys hi hs transp media charities new_car holiday tempt{
	egen `var'_sum = total(`var')
	egen `var'_sum_od = total(`var') if od_dum == 1 
	gen pct_`var' = `var'_sum_od*100/`var'_sum
}
keep if _n == 1
outreg2 using summary6`i'.doc, replace sum(detail) keep(pct_*) eqkeep(N mean)
restore
}

// Subsample by income
forvalues i =0/1{
preserve
keep if income_dum ==`i'
foreach var in groc fuel pharm ca alc rmf group recr gambling_total gaming cinema books craft fish recr_area special sa ts swim toys hi hs transp media charities new_car holiday tempt{
	egen `var'_sum = total(`var')
	egen `var'_sum_od = total(`var') if od_dum == 1 
	gen pct_`var' = `var'_sum_od*100/`var'_sum
}
keep if _n == 1
outreg2 using summary6`i'.doc, replace sum(detail) keep(pct_*) eqkeep(N mean)
restore
}

